//
//  Scan.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/3.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//

import UIKit

class Scan: UIView {

    let scan = CAShapeLayer.init()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.configLayer()
        self.configSubLayer()
    }
    
    private func configLayer() -> Void {
        let corner = CAShapeLayer.init()
        corner.frame = self.bounds
        corner.lineCap = kCALineCapRound
        corner.strokeColor = UIColor.green.cgColor
        corner.lineWidth = 10 * YXWidthScale
        let path = UIBezierPath.init()
        path.move(to: CGPoint.zero)
        path.addLine(to: CGPoint.init(x: 40 * YXWidthScale, y: 0))
        path.move(to: CGPoint.zero)
        path.addLine(to: CGPoint.init(x: 0, y: 40 * YXWidthScale))//左上
        
        path.move(to: CGPoint.init(x: self.bounds.size.width, y: 0))
        path.addLine(to: CGPoint.init(x: self.bounds.size.width - 40 * YXWidthScale, y: 0))
        path.move(to: CGPoint.init(x: self.bounds.size.width, y: 0))
        path.addLine(to: CGPoint.init(x: self.bounds.size.width, y: 40 * YXWidthScale))//右上
        
        path.move(to: CGPoint.init(x: 0, y: self.bounds.size.height))
        path.addLine(to: CGPoint.init(x: 0, y: self.bounds.size.height - 40 * YXWidthScale))
        path.move(to: CGPoint.init(x: 0, y: self.bounds.size.height))
        path.addLine(to: CGPoint.init(x: 40 * YXWidthScale, y: self.bounds.size.height))//左下
        
        path.move(to: CGPoint.init(x: self.bounds.size.width, y: self.bounds.size.height))
        path.addLine(to: CGPoint.init(x: self.bounds.size.width, y: self.bounds.size.height - 40 * YXWidthScale))
        path.move(to: CGPoint.init(x: self.bounds.size.width, y: self.bounds.size.height))
        path.addLine(to: CGPoint.init(x: self.bounds.size.width - 40 * YXWidthScale, y: self.bounds.size.height))//左下
        corner.path = path.cgPath
        self.layer.addSublayer(corner)
    }//四个绿色的角
    
    private func configSubLayer()->Void{
        scan.frame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: self.bounds.size.width, height: 20 * YXHeightScale))
        let line = UIBezierPath.init()
        line.move(to: CGPoint.init(x: 0, y: scan.frame.size.height / 2))
        line.addCurve(to: CGPoint.init(x: scan.frame.size.width, y: scan.frame.size.height / 2), controlPoint1: CGPoint.init(x: 0, y: scan.frame.size.height / 2), controlPoint2: CGPoint.init(x: scan.frame.size.width / 2, y: 0))
        line.move(to: CGPoint.init(x: 0, y: scan.frame.size.height / 2))
        line.addCurve(to: CGPoint.init(x: scan.frame.size.width, y: scan.frame.size.height / 2), controlPoint1: CGPoint.init(x: 0, y: scan.frame.size.height / 2), controlPoint2: CGPoint.init(x: scan.frame.size.width / 2, y: scan.frame.size.height))
        scan.path = line.cgPath
        scan.cornerRadius = self.bounds.size.width / 2
        let gradient = CAGradientLayer.init()
        gradient.frame = self.bounds
        gradient.colors = [UIColor.green.cgColor,UIColor.colorWithHexString(hex: "23c065").cgColor,UIColor.green.cgColor]
        gradient.locations = [NSNumber.init(value: 0),NSNumber.init(value: 0.33),NSNumber.init(value: 0.66)]
        gradient.startPoint = CGPoint.init(x: 0, y: 0)
        gradient.endPoint = CGPoint.init(x: 1, y: 0)
        self.layer.addSublayer(gradient)
        gradient.mask = scan
    }
    
    func startScan() -> Void {
        let anim = CABasicAnimation.init(keyPath: "position.y")
        anim.fromValue = NSNumber.init(value: 0)
        anim.toValue = NSNumber.init(value: Float(self.frame.size.height))
        anim.duration = 2.0
        anim.repeatCount = 100
        anim.autoreverses = false
        anim.isRemovedOnCompletion = false
        scan.add(anim, forKey: "move")
    }
    
    func stopScan() -> Void {
        scan.removeAllAnimations()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
